package cr.co.tse.padronelectoral.parser;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.SimpleFSDirectory;

import cr.co.tse.padronelectoral.DistelecRowProcessorHandler;
import cr.co.tse.padronelectoral.JuntasRowProcessorHandler;
import cr.co.tse.padronelectoral.PatronElectoralRowProcessorHandler;
import cr.co.tse.padronelectoral.domain.CodigoElectoralInscrito;
import cr.co.tse.padronelectoral.domain.Junta;

/**
 * This is the main class, to generate the index (data base)
 * @author jsanca
 *
 */
public class ParserIndexHelper {


	public ParserIndexHelper() {
		
	}
	
	
	public void parseIndex(File indexDirectoryPath, File distelecFile, File padronFiles, File dbpadron) throws CorruptIndexException,
			LockObtainFailedException, IOException {

		IndexWriter indexWriter = new IndexWriter(new SimpleFSDirectory(
				indexDirectoryPath), new StandardAnalyzer(), true,
				IndexWriter.MaxFieldLength.UNLIMITED);
		Map<Integer, CodigoElectoralInscrito> distelecMap = 
			new HashMap<Integer, CodigoElectoralInscrito> ();
		Map<String, Junta> juntaMap = 
			new HashMap<String, Junta> ();
		DistelecRowProcessorHandler distelecRowProcessorHandler = 
			new DistelecRowProcessorHandler (distelecMap );
		JuntasRowProcessorHandler dbPadronElectoralRowProcessorHandler = 
			new JuntasRowProcessorHandler (juntaMap);
		PatronElectoralRowProcessorHandler electoralRowProcessorHandler = 
			new PatronElectoralRowProcessorHandler(distelecMap, juntaMap, indexWriter);
		
		
		ParserCsvFileHelper csvFileHelper =  new ParserCsvFileHelper ();
		csvFileHelper.parser(distelecFile, distelecRowProcessorHandler);
		csvFileHelper.parser(dbpadron, dbPadronElectoralRowProcessorHandler);
		csvFileHelper.parser(padronFiles, electoralRowProcessorHandler);
		indexWriter.optimize();
		indexWriter.close();
	} // parseIndex.
	
	
	public static void main(String[] args) throws Exception {
		
		Properties properties = new Properties ();
		FileInputStream fileInputStream = new FileInputStream (new File ("inputfiles.properties"));
		ParserIndexHelper indexHelper = null;
		File padronFiles;
		File indexDirectoryPath;
		File distelecFile;
		File dbpadron;
		
		properties.load(fileInputStream);
		fileInputStream.close();

		padronFiles = new File (properties.getProperty("padron.path.file"));
		indexDirectoryPath = new File(properties.getProperty("index.path.directory"));
		distelecFile = new File (properties.getProperty("distelec.path.file"));
		dbpadron = new File (properties.getProperty("juntas.path.file"));
		
		indexHelper = new ParserIndexHelper ();
		
		System.out.println("Iniciando indexacion...");
		indexHelper.parseIndex(indexDirectoryPath, distelecFile, padronFiles, dbpadron);
		System.out.println("Indexacion terminada con exito...");
	} // main.
} // E:O:F:ParserIndexHelper.
